package classes.view.component
{
	import classes.controller.Connect;
	import classes.controller.DAOSendFlash;
	import classes.controller.DAOSendJS;
	import classes.controller.ESendJS;
	import classes.controller.EString;
	import classes.model.VOData;
	import classes.model.VOIsLocal;
	import classes.model.VOUserInfo;
	
	import com.d4p.custom.defaultButton.DefaultBtn;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	public class ChattingContainer extends Sprite
	{
		
		private var _sendBtn:DefaultBtn;
		private var _chatArea:Component_textarea; 
		private var _chatInput:TextField; 
		private var _scrollbg:Sprite;
		
		private var _connect : Connect;
		
		
		public function ChattingContainer()
		{
			setLayOut();
			init();
			defaultSetting();
			init_data(); 
			
		}
		
		public function reset():void
		{
			_chatArea.text = "";
			this._scrollbg.visible			= false;
		}
		
		
		private function setLayOut():void
		{
			this._sendBtn 		= this.getChildByName("send_mc")  		as DefaultBtn;
			this._chatInput  	= this.getChildByName("chat_input") 	as TextField;
			this._chatArea 		= this.getChildByName("chat_area")	 	as Component_textarea; 
			this._scrollbg		= this.getChildByName("scroll_bg")		as Sprite;
			
		}
		private function init():void
		{
			this._sendBtn.addEventListener(MouseEvent.CLICK, 				onSendClick); 
			this._chatInput.addEventListener(KeyboardEvent.KEY_DOWN,		onKeyboardHandler);
			this._chatArea.addEventListener("ShowScroll",					onShowScroll);
		}
		private function defaultSetting():void
		{
			this._chatInput.text = "";
			this._chatInput.multiline 		= false;
			this._chatInput.wordWrap 		= true;
			this._scrollbg.visible			= false;
			this._chatArea.editable 		= false; 
		}
		
		private function onShowScroll(e:Event):void
		{
			//scroll이 처음으로 나타났을때입니다. 
			//trace('show');
			this._scrollbg.visible			= true;
		}
		
		private function init_data():void
		{
			if(VOIsLocal.getLocalInstance().isLocal == "FILE"){
				//test.
				var loader:URLLoader = new URLLoader();
				loader.addEventListener( Event.COMPLETE,	onDataRecievedHandler);
				try {
					//loader.load( new URLRequest('sample/singleUserInfo.json'));
				}catch ( error:Error ) {
					trace( "Unable to load requested document." );
				}
			}else{
				//수정해야함
				//DAOSendJS.getLoadInstance().addEventListener(ESendJS.EVENT_CHATTING, loadComplete);
				ExternalInterface.addCallback('send_chat_to_flash',	loadComplete);
			}
			
		}
		
		
		private function loadComplete($obj:Object):void
		{
			message =$obj;
			
		}
		
		
		
		private function onDataRecievedHandler(e:Event):void
		{
			var jsonParsed:*  = JSON.parse(e.target.data);
			var obj:Object 	= jsonParsed;
			
			var id:String = VOUserInfo.getDataInstance().data.id;
			var nick:String = VOUserInfo.getDataInstance().data.nick;
			message = obj.data[0];
			
			
			
		}
		
		private function set message ($obj:Object):void
		{
			this.appendText($obj);
		}
		
		private function appendText($obj:Object):void
		{
			
			var _id :String =  $obj.data[0].id; 
			var _msg :String = $obj.data[0].msg;
			var _nick:String = $obj.data[0].nick;
			
			this._chatArea.htmlText = this._chatArea.htmlText + this.getHtmlText(_id,_msg,_nick); 
			this._chatArea.verticalScrollPosition = this._chatArea.maxVerticalScrollPosition; 
		}
		
		private function getHtmlText($id:String, $msg:String, $nick:String) :String
		{
			
			var str :String; 
			
			if($id == VOUserInfo.getDataInstance().data.id)
			{
				str = "<FONT SIZE=\"11\" COLOR=\"#CC3333\"><b>"+ $nick + " : "  + $msg + "<b>";
			}
			else
			{
				str = $nick + " : "+ $msg; 
			} 
			
			str += "\n";
			
			return str;
		}
		
		private function onKeyboardHandler(e:KeyboardEvent):void
		{
			if(e.keyCode==13){
				this.sendMessage();
				this._chatInput.text = "";
			}
		}
		
		private function onSendClick(e:MouseEvent):void
		{
			this.sendMessage();
			this._chatInput.text = "";
		}
		
		private function sendMessage():void
		{
			var _msg :String  = _chatInput.text;
			
			var obj:Object = {
				"data":[
					{
						"id":VOUserInfo.getDataInstance().data.id
						,"nick":VOUserInfo.getDataInstance().data.nick
						,"roomID":VOData.getDataInstance().roomID
						,"msg":_msg
					}
				]
			}
			
			//trace('>>>'+obj.data[0].id);
				
			//trace(VOUserInfo.getDataInstance().data.id + '>>>'+ _msg);
			message = obj;
			
			
			ExternalInterface.call('send_chat_to_js',	obj);
			
			//수정해야함
			//DAOSendFlash.getLoadInstance().setData("",{data:[{qs_no:_node.qs_no, state:_node.state}]}); 
		}
	}
}